home *** CD-ROM | disk | FTP | other *** search
/ Isometric Game Programming with DirectX 7.0 / Isometric Game Programming.iso / source / chapter3 / isohex3_3 / isohex3_3.cpp next >
Encoding:
C/C++ Source or Header  |  2000-05-17  |  6.8 KB  |  274 lines

  1. /*****************************************************************************
  2. IsoHex3_3.cpp
  3. Ernest S. Pazera
  4. 08APR2000
  5. Start a WIN32 Application Workspace, add in this file
  6. No other libs are required
  7. *****************************************************************************/
  8.  
  9. //////////////////////////////////////////////////////////////////////////////
  10. //INCLUDES
  11. //////////////////////////////////////////////////////////////////////////////
  12. #define WIN32_LEAN_AND_MEAN  
  13.  
  14. #include <windows.h>   
  15.  
  16. //////////////////////////////////////////////////////////////////////////////
  17. //DEFINES
  18. //////////////////////////////////////////////////////////////////////////////
  19. //name for our window class
  20. #define WINDOWCLASS "ISOHEX3"
  21. //title of the application
  22. #define WINDOWTITLE "IsoHex 3-3"
  23.  
  24. //////////////////////////////////////////////////////////////////////////////
  25. //PROTOTYPES
  26. //////////////////////////////////////////////////////////////////////////////
  27. bool Prog_Init();//game data initalizer
  28. void Prog_Loop();//main game loop
  29. void Prog_Done();//game clean up
  30.  
  31. //////////////////////////////////////////////////////////////////////////////
  32. //GLOBALS
  33. //////////////////////////////////////////////////////////////////////////////
  34. HINSTANCE hInstMain=NULL;//main application handle
  35. HWND hWndMain=NULL;//handle to our main window
  36. //pens, old and new
  37. HPEN hpenNew=NULL;
  38. HPEN hpenOld=NULL;
  39. //region
  40. HRGN hrgnClip=NULL;
  41.  
  42. //////////////////////////////////////////////////////////////////////////////
  43. //WINDOWPROC
  44. //////////////////////////////////////////////////////////////////////////////
  45. LRESULT CALLBACK TheWindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
  46. {
  47.     //which message did we get?
  48.     switch(uMsg)
  49.     {
  50.     case WM_DESTROY://the window is being destroyed
  51.         {
  52.  
  53.             //tell the application we are quitting
  54.             PostQuitMessage(0);
  55.  
  56.             //handled message, so return 0
  57.             return(0);
  58.  
  59.         }break;
  60.     case WM_PAINT://the window needs repainting
  61.         {
  62.             //a variable needed for painting information
  63.             PAINTSTRUCT ps;
  64.             
  65.             //start painting
  66.             HDC hdc=BeginPaint(hwnd,&ps);
  67.  
  68.             /////////////////////////////
  69.             //painting code would go here
  70.             /////////////////////////////
  71.  
  72.             //end painting
  73.             EndPaint(hwnd,&ps);
  74.                         
  75.             //handled message, so return 0
  76.             return(0);
  77.         }break;
  78.     }
  79.  
  80.     //pass along any other message to default message handler
  81.     return(DefWindowProc(hwnd,uMsg,wParam,lParam));
  82. }
  83.  
  84.  
  85. //////////////////////////////////////////////////////////////////////////////
  86. //WINMAIN
  87. //////////////////////////////////////////////////////////////////////////////
  88. int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
  89. {
  90.     //assign instance to global variable
  91.     hInstMain=hInstance;
  92.  
  93.     //create window class
  94.     WNDCLASSEX wcx;
  95.  
  96.     //set the size of the structure
  97.     wcx.cbSize=sizeof(WNDCLASSEX);
  98.  
  99.     //class style
  100.     wcx.style=CS_OWNDC | CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
  101.  
  102.     //window procedure
  103.     wcx.lpfnWndProc=TheWindowProc;
  104.  
  105.     //class extra
  106.     wcx.cbClsExtra=0;
  107.  
  108.     //window extra
  109.     wcx.cbWndExtra=0;
  110.  
  111.     //application handle
  112.     wcx.hInstance=hInstMain;
  113.  
  114.     //icon
  115.     wcx.hIcon=LoadIcon(NULL,IDI_APPLICATION);
  116.  
  117.     //cursor
  118.     wcx.hCursor=LoadCursor(NULL,IDC_ARROW);
  119.  
  120.     //background color
  121.     wcx.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
  122.  
  123.     //menu
  124.     wcx.lpszMenuName=NULL;
  125.  
  126.     //class name
  127.     wcx.lpszClassName=WINDOWCLASS;
  128.  
  129.     //small icon
  130.     wcx.hIconSm=NULL;
  131.  
  132.     //register the window class, return 0 if not successful
  133.     if(!RegisterClassEx(&wcx)) return(0);
  134.  
  135.     //create main window
  136.     hWndMain=CreateWindowEx(0,WINDOWCLASS,WINDOWTITLE, WS_BORDER | WS_SYSMENU | WS_VISIBLE,0,0,320,240,NULL,NULL,hInstMain,NULL);
  137.  
  138.     //error check
  139.     if(!hWndMain) return(0);
  140.  
  141.     //if program initialization failed, then return with 0
  142.     if(!Prog_Init()) return(0);
  143.  
  144.     //message structure
  145.     MSG msg;
  146.  
  147.     //message pump
  148.     for(;;)    
  149.     {
  150.         //look for a message
  151.         if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  152.         {
  153.             //there is a message
  154.  
  155.             //check that we arent quitting
  156.             if(msg.message==WM_QUIT) break;
  157.             
  158.             //translate message
  159.             TranslateMessage(&msg);
  160.  
  161.             //dispatch message
  162.             DispatchMessage(&msg);
  163.         }
  164.  
  165.         //run main game loop
  166.         Prog_Loop();
  167.     }
  168.     
  169.     //clean up program data
  170.     Prog_Done();
  171.  
  172.     //return the wparam from the WM_QUIT message
  173.     return(msg.wParam);
  174. }
  175.  
  176. //////////////////////////////////////////////////////////////////////////////
  177. //INITIALIZATION
  178. //////////////////////////////////////////////////////////////////////////////
  179. bool Prog_Init()
  180. {
  181.     //create a solid red pen
  182.     hpenNew=CreatePen(PS_SOLID,0,RGB(255,0,0));
  183.  
  184.     //retrieve the client rectangle for the window
  185.     RECT rcClient;
  186.     GetClientRect(hWndMain,&rcClient);
  187.  
  188.     //create an elliptical region
  189.     //hrgnClip=CreateEllipticRgn(0,0,rcClient.right,rcClient.bottom);
  190.  
  191.     POINT ptVertice[4];
  192.     ptVertice[0].x=rcClient.right/2;
  193.     ptVertice[0].y=0;
  194.     ptVertice[1].x=rcClient.right;
  195.     ptVertice[1].y=rcClient.bottom/2;
  196.     ptVertice[2].x=rcClient.right/2;
  197.     ptVertice[2].y=rcClient.bottom;
  198.     ptVertice[3].x=0;
  199.     ptVertice[3].y=rcClient.bottom/2;
  200.  
  201.     hrgnClip=CreatePolygonRgn(ptVertice,4,ALTERNATE);
  202.     
  203.     //borrow the dc from the main window
  204.     HDC hdc=GetDC(hWndMain);
  205.  
  206.     //select the new pen into the dc, and keep the old one
  207.     hpenOld=(HPEN)SelectObject(hdc,hpenNew);
  208.  
  209.     //select the clipping region into the dc
  210.     SelectObject(hdc,hrgnClip);
  211.  
  212.     //make vertical stripes
  213.     int nStripeX=rcClient.right/10;
  214.     int nCount;
  215.  
  216.     //loop through and draw the stripes
  217.     for(nCount=0;nCount<10;nCount++)
  218.     {
  219.         //moveto the top of the client area
  220.         MoveToEx(hdc,nStripeX*nCount,0,NULL);
  221.  
  222.         //line to the bottom of the client area
  223.         LineTo(hdc,nStripeX*nCount,rcClient.bottom);
  224.     }
  225.  
  226.     //make the horizontal stripes
  227.     int nStripeY=rcClient.bottom/10;
  228.  
  229.     //loop through and draw the stripes
  230.     for(nCount=0;nCount<10;nCount++)
  231.     {
  232.         //move to the left of the client area
  233.         MoveToEx(hdc,0,nStripeY*nCount,NULL);
  234.  
  235.         //line to the right of the client area
  236.         LineTo(hdc,rcClient.right,nStripeY*nCount);
  237.     }
  238.  
  239.     //return the borrowed dc to the system
  240.     ReleaseDC(hWndMain,hdc);
  241.  
  242.     return(true);//return success
  243. }
  244.  
  245. //////////////////////////////////////////////////////////////////////////////
  246. //CLEANUP
  247. //////////////////////////////////////////////////////////////////////////////
  248. void Prog_Done()
  249. {
  250.     //borrow the dc from the main window
  251.     HDC hdc=GetDC(hWndMain);
  252.  
  253.     //restore the old pen
  254.     SelectObject(hdc,hpenOld);
  255.  
  256.     //return the dc to the system
  257.     ReleaseDC(hWndMain,hdc);
  258.  
  259.     //delete our gdi objects
  260.     DeleteObject(hrgnClip);
  261.     DeleteObject(hpenNew);
  262. }
  263.  
  264. //////////////////////////////////////////////////////////////////////////////
  265. //MAIN GAME LOOP
  266. //////////////////////////////////////////////////////////////////////////////
  267. void Prog_Loop()
  268. {
  269.     ///////////////////////////
  270.     //main game logic goes here
  271.     ///////////////////////////
  272. }
  273.  
  274.